<?php
// WARNING: this will work only in PHP5
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
if(!defined('ACC_INC')) define(ACC_INC,DOKU_INC.'phpincludes/');
require_once(ACC_INC.'ACC_Form_Base.php');

/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-01-22
 */
class ACC_Form_eLetter extends ACC_Form_Base {

    var $mailTo = 'web@accmontreal.ca';
    var $mailToAllowed = array('web@accmontreal.ca');
    var $mailToField = 'to_email';
    var $mailFromField = 'from_email';
    var $mailFromNameField = 'from_name';
    var $mailSubjectPrefix = '[ACC eLetter Item] ';

    var $mailSubjectFmtName = 'mailSubjectFmt';
    var $mailBodyFmtName = 'mailBodyFmt';

    var $metaFileSuffix = 'eletter';
    var $metaFileid = '__eletter';

    var $adminmode = false;

    var $langArray = array(
        'fr' => array(
            'mailSent'       => 'Votre message a &eacute;t&eacute; envoy&eacute;',
            'mailSentError'  => "Erreur d'envoie, SVP contacter l'administrateur a "
        ),
        'en' => array(
            'mailSent'       => 'Your message was sent sucessfully',
            'mailSentError'  => 'There was an error sending your email, please contact the admin at ',
            'formDescFmt' => 'name accmtl_eletter_add
action mail web@accmontreal.ca
Thanks "Your eLetter message was recorded sucessfully and will be included in the next ACC Montreal eLetter. Thank you!"
Fieldset "Contact Details"
const uid __uid__
Textbox "Name" #80 &from_name
email "e-mail" #80 &from_email
phone "Phone (optional)" #80 &from_phone !
Fieldset "News Details English"
Textbox "Subject" #80 &subject_en
textarea "eMail Message (Please keep it short!)" x5 #1000 &msg_en
textarea "Online Message (optional)" x10 #5000 &msg2_en !
link "A link (optional)" #256 &link_en !
Fieldset "News Details French"
Textbox "Suject" #80 &subject_fr
textarea "Message email (SVP soyez concis!)" x5 __msg_max_char__ &msg_fr
textarea "Message online (optionel)" x10 __msg2_max_char__ &msg2_fr !
Static "Above Link (english section) will be used if not provided here."
link "A link (optionel)" #256 &link_fr !
Fieldset "Image, News Lifetime and Submission"
static "You may provide an image to illustrate your news item, please keep it very small (the image  will be resized to a max of 180x180 pixels anyway)."
file "Image/File (optional)" &imgfile !
static "By default, a news is valid from now and expires in 7 days. You may want to specify different dates. This will affect in which eLetter(s) the message will be posted."
date "Valid From [YYYY-MM-DD]" "=__date0__" ">__>date0__" "<__<date0__" #20 &date_from
date "Expires After [YYYY-MM-DD]" "=__date1__" ">__>date1__" "<__<date1__" #20 &date_to
submit "Add your News to the Pool" &submit_news_item',
            'mailSubjectFmt' => ' __subject_en__',
            'mailBodyFmt' => '__ACC_Form_list_all__'
        )
    );

    function _getFormDescFieldsVals($formDescFmtName) {
        //only one possible format, ignore $formDescFmtName
        $nowStamp = mktime(0, 0, 0, date("m")  , date("d"), date("Y"));
        $now = date('Y-m-d',$nowStamp);
        $dtime6d  = 3600*6*24;
        $edefdate = date('Y-m-d',$nowStamp + $dtime6d);;
        if($this->adminmode) {
            $maxchar1 = '';
            $maxchar2 = '';
            $dtime1d  = 3600*1*24;
            $dtime3mo = 3600*24*92; // 3 months (approx)
            $bmindate = date('Y-m-d',$nowStamp - 3600*24*365);
            $emindate = date('Y-m-d',$nowStamp - 3600*24*365);
            $maxdate  = date('Y-m-d',$nowStamp + 3600*24*365);
       } else {
            $maxchar1 = '#1000';
            $maxchar2 = '#5000';
            $dtime1d  = 3600*1*24;
            $dtime3mo = 3600*24*92; // 3 months (approx)
            $bmindate = $now;
            $emindate = date('Y-m-d',$nowStamp + $dtime1d);
            $maxdate  = date('Y-m-d',$nowStamp + $dtime3mo);
        }

        //Need to set all values in $this->formDescFields
        return array(
            '__msg_max_char__'  => $maxchar1,
            '__msg2_max_char__' => $maxchar2,
            '__uid__' => uniqid(),
            '__date0__' => $now,
            '__>date0__' => $bmindate,
            '__<date0__' => $maxdate,
            '__date1__' => $edefdate,
            '__>date1__' => $emindate,
            '__<date1__' => $maxdate
        );
    }

    function _postcheckpost($data) {
        global $ID;
        $errors = array();
        $values = $this->buro->_collectpost($data);
        $idx = 999;

        //TODO: may want to do some more checking
        return $errors;
    }



    function _preActionValuesMods($values) {
        if(!isset($values['now'])) $values['now'] = date('c');
        if(!isset($values['status'])) $values['status'] = 'new';
        return $values;
    }


    function _action_ok($data,$thanks) {
        $values = $this->buro->_collectpost($data);
        $values = $this->_preActionValuesMods($values);

        $metaFile = $this->_metaFile_get($this->metaFileid);
        $metaFile = $this->_metaFile_add($metaFile,$values) ;
        $ok = $this->_metaFile_save($metaFile,$this->metaFileid);

        $ok = $this->_sendMail($values);

        if ($ok) return $thanks;
        return false;
    }


}

/*
Add Item mode
- add date picker
- add existing img selection to form (mediamanager.php) + selected img name/stamp
- if from admin mode: get data from file/uid; use provided uid; set action to return to admin list mode; remove date llimitations
Admin mode
1) list with sub select and action
- select items according to vdate&edate
- change sort order
- change status [new/ignore/post...]
- change priority
- edit item
2) edit item mode
- get item values from file for uid
- set post action to return to list admin mode
3) show eLetter and code
*/